﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {


        public int[,] board; // board two dimentional array
        


        public Form1()
        {
            InitializeComponent();
            board = new int[8, 8];
            // setup red piceces
            board[0,0] = 1;
            board[2,0] = 1;
            board[4,0] = 1;
            board[6,0] = 1;
            board[1,1] = 1;
            board[3,1] = 1;
            board[5,1] = 1;
            board[7,1] = 1;
            board[0,2] = 1;
            board[2,2] = 1;
            board[4,2] = 1;
            board[6,2] = 1;

            // setup black piceces
            board[1, 7] = -1;
            board[3, 7] = -1;
            board[5, 7] = -1;
            board[7, 7] = -1;
            board[0, 6] = -1;
            board[2, 6] = -1;
            board[4, 6] = -1;
            board[6, 6] = -1;
            board[1, 5] = -1;
            board[3, 5] = -1;
            board[5, 5] = -1;
            board[7, 5] = -1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Size = new System.Drawing.Size(460, 480);
            updateBoard();

            
        }

        //this method draws the board
        protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Get the graphics object 
            Graphics gfx = paintEvnt.Graphics;
            // Create a new pen that we shall use for drawing the line 
            Pen myPen = new Pen(Color.Black);
            // Loop and create a horizontal line 50 pixels below the last one 
            for (int i = 20; i <= 460; i = i + 50)
            {
                gfx.DrawLine(myPen, 20, i, 420, i);
            }
            // Loop and create a vertical line 50 pixels next to the last one 
            for (int x = 20; x < 460; x = x + 50)
            {
                gfx.DrawLine(myPen, x, 20, x, 420);
            }
        }

        //this method places the pieces
        public void updateBoard()
        {

            for (int x = 0; x < board.GetLength(0); x++)
            {
                for (int y = 0; y < board.GetLength(1); y++)
                {
                    if (board[x, y] == 1)
                    {
                        PictureBox redPiece = new PictureBox();
                        redPiece.Size = new Size(48, 48);
                        redPiece.Image = Properties.Resources.red;
                        redPiece.Location = new Point(22 + (x * 48) + (x * 2), 22 + (y * 48) + (y * 2));
                        this.Controls.Add(redPiece);
                        redPiece.Name = "red_" + x.ToString() + "-" + y.ToString();
                        redPiece.Cursor = System.Windows.Forms.Cursors.Hand;
                        redPiece.Click += new EventHandler(redPiece_Click);
                        redPiece.MouseDown += new MouseEventHandler(redPiece_MouseDown);
                    }
                    if (board[x, y] == -1)
                    {
                        PictureBox blackPiece = new PictureBox();
                        blackPiece.Size = new Size(48, 48);
                        blackPiece.Image = Properties.Resources.black;
                        blackPiece.Location = new Point(22 + (x * 48) + (x * 2), 22 + (y * 48) + (y * 2));
                        this.Controls.Add(blackPiece);
                        blackPiece.Name = "black_" + x.ToString() + "-" + y.ToString();
                        blackPiece.Cursor = System.Windows.Forms.Cursors.Hand;
                        blackPiece.Click += new EventHandler(blackPiece_Click);
                        blackPiece.MouseDown += new MouseEventHandler(blackPiece_MouseDown);
                    }
                }
            }
        }

        void redPiece_MouseDown(object sender, MouseEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("redPiece MouseDown event called.");
        }

        void redPiece_Click(object sender, EventArgs e)
        {
            PictureBox clickedPiece = (PictureBox)sender;
            MessageBox.Show(this, clickedPiece.Name);
        }

        void blackPiece_MouseDown(object sender, MouseEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("blackPiece MouseDown event called.");
        }

        void blackPiece_Click(object sender, EventArgs e)
        {
            PictureBox clickedPiece = (PictureBox)sender;
            MessageBox.Show(this, clickedPiece.Name);
        }

        
    }

}
